home *** CD-ROM | disk | FTP | other *** search
- /*
- * Error.c
- *
- * -- by Michael Hecht (Michael_Hecht@mac.sas.com)
- * public domain
- */
-
-
- /* This part goes in a header file someplace */
- void Substitute( StringPtr targetStr, ConstStr255Param find, ConstStr255Param replace );
- void GetErrorString( OSErr err, StringPtr errorStr );
-
- enum {
- kErrorStrList = 128,
- kUnknownErrorStr = 1
- };
-
-
- /* Locate the "find" string in the "targetStr" and replace it with "replace" */
- void Substitute( StringPtr targetStr, ConstStr255Param find, ConstStr255Param replace )
- {
- StringHandle target;
- Size targetSize;
-
-
- target = NewString( targetStr );
- if( !target )
- return;
-
- /* Perform the substitution */
- Munger(( Handle )target, 1, find + 1, *find, replace + 1, *replace );
-
- /* Put it back in the string */
- HLock(( Handle )target );
- targetSize = GetHandleSize(( Handle )target ) - 1;
- if( targetSize > 255 )
- targetSize = 255;
- **target = targetSize;
- BlockMove( *target, targetStr, targetSize + 1 );
- DisposHandle(( Handle )target );
- }
-
- /* Definition of the 'ERR#' resource structure */
- typedef struct {
- OSErr loRange, hiRange;
- int strID;
- } ErrorMapRecord, *ErrorMapPtr;
-
- typedef struct {
- int count;
- ErrorMapRecord error[];
- } ErrorListRecord, *ErrorListPtr, **ErrorListHandle;
-
-
- void GetErrorString( OSErr err, StringPtr errorStr )
- {
- ErrorListHandle theErrorList;
- register int i, n;
- register ErrorMapPtr e;
- Str255 tempStr;
-
-
- /* Assume we won't find a message */
- *errorStr = 0;
-
- /* Access the error mapping list */
- theErrorList = ( ErrorListHandle )Get1Resource( 'ERR#', 128 );
- if( theErrorList ) {
-
- /* Set up for loop */
- n = ( *theErrorList )->count;
- e = ( *theErrorList )->error;
-
- /* Go through the list of ranges */
- for( i = 0; i < n; i++, e++ ) {
-
- /* If not in this range, skip */
- if( err > e->hiRange )
- continue;
-
- /* If range not found, break */
- if( err < e->loRange )
- break;
-
- /* Found it! Get the message */
- GetIndString( errorStr, e->strID, err - e->loRange + 1 );
- break;
- }
- }
-
- /* If no message, use "unknown error" message */
- if( *errorStr == 0 ) {
-
- GetIndString( errorStr, kErrorStrList, kUnknownErrorStr );
-
- /* Substitute the error number into the message */
- NumToString(( long )err, tempStr );
- Substitute(( StringPtr )errorStr, ( StringPtr )"\p^ERR", tempStr );
- }
- }
-